﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mmoss.configuration;
using mmoss.classes;

namespace mmoss.application.classes
{
    public class KeystrokeMessageFilter : System.Windows.Forms.IMessageFilter
    {
        private int count = 0;
        public delegate void KeyDelegate(object sender, KeyEventArgs e);
        public delegate void TimerDelegate(object sender, EventArgs e);
        public delegate void CCScannedGood(CCCard l_Card);
        public delegate void CardScannedBad(string l_message);
        public delegate void LicenseScannedGood(DriversLicense l_License);
        public delegate void CharacterRead(string s_buffer);
        public event CCScannedGood CCScanned;
        public event CharacterRead CardCharRead;
        public event LicenseScannedGood LicenseScanned;
        public event CardScannedBad CardBadScan;
        public string KeyBuffer = "";
        Timer KeyTimeOut = new Timer();
        private bool ScanInProgress = false;

        public KeystrokeMessageFilter()
        {
            KeyTimeOut.Tick += new EventHandler(KeyTimeOut_Tick);
            this.CCScanned += new CCScannedGood(KeystrokeMessageFilter_CCScanned);
            this.CardBadScan += new CardScannedBad(KeystrokeMessageFilter_CardBadScan);
            this.LicenseScanned += new LicenseScannedGood(KeystrokeMessageFilter_LicenseScanned);
            this.CardCharRead += new CharacterRead(KeystrokeMessageFilter_CardCharRead);
        }

        void KeystrokeMessageFilter_CardCharRead(string s_buffer)
        {
            //placeholder
        }

        void KeystrokeMessageFilter_LicenseScanned(DriversLicense l_License)
        {
            //placeholder
        }

        void KeystrokeMessageFilter_CardBadScan(string l_message)
        {
            //placeholder
        }

        void KeystrokeMessageFilter_CCScanned(CCCard l_Card)
        {
            //this is a placeholder
        }

        public static void keyItemDown(object sender, KeyEventArgs e)
        {

        }

        public void KeyTimeOut_Tick(object sender, EventArgs e)
        {

            //timed out
            ScanInProgress = false;
            KeyTimeOut.Enabled = false;
            KeyTimeOut.Stop();
            //show buffer
            //MessageBox.Show("buffer=" + KeyBuffer);
            //create the appropropriate object type
            //todo - make this more generic a routing
            parseCard(KeyBuffer);
        }

        #region Implementation of IMessageFilter
        public bool PreFilterMessage(ref Message m)
        {
            string keyRead = "";
            if ((m.Msg == 256 /*0x0100*/))
            {
                count++;
                if (count == 19)
                {
                    string sTest = "now";
                }
                switch ((int)m.WParam)
                {
                    case (int)Keys.A:
                        keyRead = "A";
                        break;
                    case (int)Keys.B:
                        keyRead = "B";
                        break;
                    case (int)Keys.C:
                        keyRead = "C";
                        break;
                    case (int)Keys.D:
                        keyRead = "D";
                        break;
                    case (int)Keys.E:
                        keyRead = "E";
                        break;
                    case (int)Keys.F:
                        keyRead = "F";
                        break;
                    case (int)Keys.G:
                        keyRead = "G";
                        break;
                    case (int)Keys.H:
                        keyRead = "H";
                        break;
                    case (int)Keys.I:
                        keyRead = "I";
                        break;
                    case (int)Keys.J:
                        keyRead = "J";
                        break;
                    case (int)Keys.K:
                        keyRead = "K";
                        break;
                    case (int)Keys.L:
                        keyRead = "L";
                        break;
                    case (int)Keys.M:
                        keyRead = "M";
                        break;
                    case (int)Keys.N:
                        keyRead = "N";
                        break;
                    case (int)Keys.O:
                        keyRead = "O";
                        break;
                    case (int)Keys.P:
                        keyRead = "P";
                        break;
                    case (int)Keys.Q:
                        keyRead = "Q";
                        break;
                    case (int)Keys.R:
                        keyRead = "R";
                        break;
                    case (int)Keys.S:
                        keyRead = "S";
                        break;
                    case (int)Keys.T:
                        keyRead = "T";
                        break;
                    case (int)Keys.U:
                        keyRead = "U";
                        break;
                    case (int)Keys.V:
                        keyRead = "V";
                        break;
                    case (int)Keys.W:
                        keyRead = "W";
                        break;
                    case (int)Keys.X:
                        keyRead = "X";
                        break;
                    case (int)Keys.Y:
                        keyRead = "Y";
                        break;
                    case (int)Keys.Z:
                        keyRead = "Z";
                        break;
                    case (int)Keys.NumPad0:
                        keyRead = "0";
                        break;
                    case (int)Keys.NumPad1:
                        keyRead = "1";
                        break;
                    case (int)Keys.NumPad2:
                        keyRead = "2";
                        break;
                    case (int)Keys.NumPad3:
                        keyRead = "3";
                        break;
                    case (int)Keys.NumPad4:
                        keyRead = "4";
                        break;
                    case (int)Keys.NumPad5:
                        keyRead = "5";
                        break;
                    case (int)Keys.NumPad6:
                        keyRead = "6";
                        break;
                    case (int)Keys.NumPad7:
                        keyRead = "7";
                        break;
                    case (int)Keys.NumPad8:
                        keyRead = "8";
                        break;
                    case (int)Keys.NumPad9:
                        keyRead = "9";
                        break;
                    case (int)(Keys.D0):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = ")";
                        }
                        else
                        {
                            keyRead = "0";
                        }
                        break;
                    case (int)(Keys.D1):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "!";
                        }
                        else
                        {
                            keyRead = "1";
                        }
                        break;
                    case (int)(Keys.D2):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "@";
                        }
                        else
                        {
                            keyRead = "2";
                        }
                        break;
                    case (int)(Keys.D3):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "#";
                        }
                        else
                        {
                            keyRead = "3";
                        }
                        break;
                    case (int)(Keys.D4):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "$";
                        }
                        else
                        {
                            keyRead = "4";
                        }
                        break;
                    case (int)(Keys.D5):
                       // if ((((int)m.WParam)|(int)Control.ModifierKeys)==((int)(Keys.D5|Keys.Shift)))
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            KeyTimeOut.Interval = 500;
                            KeyTimeOut.Start();
                            KeyBuffer = "";
                            keyRead = "%";
                            ScanInProgress = true;
                            break;
                        }
                        else
                        {
                            keyRead = "5";
                            break;
                        }
                    case (int)(Keys.D6):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "^";
                        }
                        else
                        {
                            keyRead = "6";
                        }
                        break;
                    case (int)(Keys.D7):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "&";
                        }
                        else
                        {
                            keyRead = "7";
                        }
                        break;
                    case (int)(Keys.D8):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "*";
                        }
                        else
                        {
                            keyRead = "8";
                        }
                        break;
                    case (int)(Keys.D9):
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "(";
                        }
                        else
                        {
                            keyRead = "9";
                        }
                        break;
                    case (int)Keys.OemQuestion:
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "?";
                        }
                        else
                        {
                            keyRead = "/";
                        }
                        break;
                    case (int)Keys.Oemplus:
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = "+";
                        }
                        else
                        {
                            keyRead = "=";
                        }
                        break;

                    case (int)Keys.Space:
                        keyRead = " ";
                        break;
                    case (int)Keys.OemSemicolon:
                        if ((int)(Control.ModifierKeys & Keys.Shift) == (int)Keys.Shift)
                        {
                            keyRead = ":";
                        }
                        else
                        {
                            keyRead = ";";
                        }
                        break;
                    case ((int)Keys.Return):
                        //carriage return
                        ScanInProgress = false;
                        return false;
                        break;
                    default:
                        if ((((int)m.WParam) | ((int)Control.ModifierKeys)) != (int)Keys.ShiftKey)
                        {
                            //keyRead = ((char)m.WParam.ToInt32()).ToString();
                        }
                        break;
                }
                if (ScanInProgress)
                {
                    count++;
                    //KeyBuffer += (((int)m.WParam) | ((int)Control.ModifierKeys)).ToString()+ "|";
                    //Keys l_key = ((Keys)m.WParam.ToInt32()) & Keys.KeyCode;
                    //KeyBuffer = KeyBuffer + ((char)l_key);
                    KeyBuffer = KeyBuffer + keyRead;
                    //raise event for listeners
                    CardCharRead(KeyBuffer);
                    //reset timer
                    KeyTimeOut.Stop();
                    KeyTimeOut.Start();
                    return false;
                }
            }
            return false;
        }
        private void  parseCard(string sCardInfo)
        {
            if (sCardInfo.Substring(0, 3) == "%CO")
            {
                //drivers license
                DriversLicense l_license = new DriversLicense(sCardInfo);
                if (l_license.ParseError == null)
                {
                    //kick off event
                    LicenseScanned(l_license);
                }
            }
            else
            {
                CCCard l_card = new CCCard(sCardInfo);
                if (l_card.ParseError == null)
                {
                    CCScanned(l_card);
                }
            }
        }
    }
}
        #endregion